package com.example.blog.controller;

import com.example.blog.Service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Slf4j
@Controller
public class RegisterCtl {

    @Autowired
    private UserService userService;
    @GetMapping("/register")
    public String Show(){
        return "/register";
    }
    @PostMapping("/register")
    public String register(@RequestParam String username,
                           @RequestParam String password,
                           @RequestParam String gender,
                           @RequestParam int age,
                           RedirectAttributes redirectAttributes) {
        int result = userService.register(username, password, gender, age);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("success", "注册成功，请登录！");
            return "login";
        } else {
            redirectAttributes.addFlashAttribute("error", "注册失败，可能是账号已存在！");
            return "register";
        }
    }
}